package com.hui.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hui.handler.exception.SystemException;
import com.hui.mapper.UserRechargeMapper;
import com.hui.entity.dao.ShopUser;
import com.hui.entity.dao.UserRecharge;
import com.hui.entity.enums.AppHttpCodeEnum;
import com.hui.entity.enums.OrderInfoEnum;
import com.hui.entity.enums.PayTypeEnum;
import com.hui.service.UserRechargeService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 用户充值表(UserRecharge)表服务实现类
 *
 * @author hui
 * @since 2022-08-03 17:24:37
 */
@Service("userRechargeService")
public class UserRechargeServiceImpl extends ServiceImpl<UserRechargeMapper, UserRecharge> implements UserRechargeService {
    /**
     * @param user      用户信息
     * @param price     充值金额
     * @param paidPrice 赠送金额
     * @return
     */
    @Override
    public String addRecharge(ShopUser user, String price, String paidPrice) {
        if (!StrUtil.isBlank(price) && !StrUtil.isBlank(paidPrice)) {
            UserRecharge userRecharge = new UserRecharge();
            String orderSn = IdUtil.getSnowflake(0L, 0L).nextIdStr();
            userRecharge.setNickname(user.getNickname());
            userRecharge.setOrderId(orderSn);
            userRecharge.setUid(user.getUid());
            userRecharge.setPrice(new BigDecimal(price));
            userRecharge.setGivePrice(new BigDecimal(paidPrice));
            userRecharge.setRechargeType(PayTypeEnum.WEIXIN.getValue());
            userRecharge.setPaid(OrderInfoEnum.PAY_STATUS_0.getValue());
            save(userRecharge);
            return orderSn;
        } else {
            throw new SystemException(AppHttpCodeEnum.NO_PARAMETE_OPERATE.getCode(), "参数非法");
        }
    }
}

